<template>
  <div class="movie-detail-view">
    <!-- 导航栏 -->
    <nav class="navbar">
      <div class="nav-container">
        <div class="nav-brand">
          <h2>电影推荐系统</h2>
        </div>
        
        <div class="nav-menu">
          <router-link to="/" class="nav-link">
            首页
          </router-link>
          <router-link to="/movies" class="nav-link">
            电影库
          </router-link>
          <router-link to="/recommendations" class="nav-link">
            推荐
          </router-link>
          <router-link to="/profile" class="nav-link">
            个人中心
          </router-link>
        </div>
        
        <div class="nav-user">
          <span class="user-name">{{ authStore.user?.username }}</span>
          <button @click="handleLogout" class="logout-btn">
            退出
          </button>
        </div>
      </div>
    </nav>
    
    <!-- 主要内容 -->
    <main class="main-content">
      <!-- 返回按钮 -->
      <div class="back-button">
        <button @click="goBack" class="back-btn">
          ← 返回
        </button>
      </div>
      
      <!-- 加载状态 -->
      <div v-if="moviesStore.isLoading" class="loading">
        <div class="loading-spinner"></div>
        <p>正在加载电影详情...</p>
      </div>
      
      <!-- 电影详情 -->
      <div v-else-if="moviesStore.currentMovie" class="movie-detail">
        <div class="movie-header">
          <div class="movie-info">
            <h1 class="movie-title">{{ moviesStore.currentMovie.title }}</h1>
            <p class="movie-year">{{ moviesStore.currentMovie.year }}</p>
            
            <div class="movie-genres">
              <span
                v-for="genre in moviesStore.currentMovie.genres"
                :key="genre.id"
                class="genre-tag"
              >
                {{ genre.name }}
              </span>
            </div>
            
            <div class="movie-stats">
              <div class="stat-item">
                <span class="stat-label">平均评分:</span>
                <span class="stat-value rating">{{ moviesStore.currentMovie.average_rating.toFixed(1) }}</span>
              </div>
              <div class="stat-item">
                <span class="stat-label">电影ID:</span>
                <span class="stat-value">{{ moviesStore.currentMovie.movie_id }}</span>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 用户评分区域 -->
        <div class="rating-section">
          <h2>您的评分</h2>
          
          <div v-if="userRating" class="current-rating">
            <p>您已为此电影评分: <strong>{{ userRating.rating }}</strong></p>
            <p class="rating-time">评分时间: {{ formatDate(userRating.timestamp) }}</p>
          </div>
          
          <div class="rating-form">
            <label for="rating">{{ userRating ? '修改评分:' : '为此电影评分:' }}</label>
            <div class="rating-input">
              <select
                id="rating"
                v-model="newRating"
                class="rating-select"
              >
                <option value="">选择评分</option>
                <option v-for="i in 5" :key="i" :value="i">{{ i }} 星</option>
              </select>
              
              <button
                @click="submitRating"
                :disabled="!newRating || ratingsStore.isLoading"
                class="submit-rating-btn"
              >
                <span v-if="ratingsStore.isLoading">提交中...</span>
                <span v-else>{{ userRating ? '更新评分' : '提交评分' }}</span>
              </button>
              
              <button
                v-if="userRating"
                @click="deleteRating"
                :disabled="ratingsStore.isLoading"
                class="delete-rating-btn"
              >
                删除评分
              </button>
            </div>
          </div>
          
          <div v-if="ratingsStore.error" class="error-message">
            {{ ratingsStore.error }}
          </div>
        </div>
        
        <!-- 电影留言区域 -->
        <div class="comments-section">
          <h2>电影留言</h2>
          
          <!-- 发表留言表单 -->
          <div class="comment-form">
            <h3>发表留言</h3>
            <div class="form-group">
              <textarea
                v-model="newComment"
                placeholder="分享您对这部电影的看法..."
                class="comment-textarea"
                rows="4"
                maxlength="1000"
              ></textarea>
              <div class="comment-actions">
                <span class="char-count">{{ newComment.length }}/1000</span>
                <button
                  @click="submitComment"
                  :disabled="!newComment.trim() || newComment.trim().length < 5 || isSubmittingComment"
                  class="submit-comment-btn"
                >
                  <span v-if="isSubmittingComment">发布中...</span>
                  <span v-else>发布留言</span>
                </button>
              </div>
            </div>
            
            <div v-if="commentError" class="error-message">
              {{ commentError }}
            </div>
          </div>
          
          <!-- 留言列表 -->
          <div class="comments-list">
            <div v-if="isLoadingComments" class="loading-comments">
              <div class="loading-spinner"></div>
              <p>正在加载留言...</p>
            </div>
            
            <div v-else-if="comments.length > 0" class="comments-container">
              <div
                v-for="comment in comments"
                :key="comment.id"
                class="comment-item"
              >
                <div class="comment-header">
                  <span class="comment-author">{{ comment.user_name }}</span>
                  <span class="comment-time">{{ formatDate(comment.timestamp) }}</span>
                  
                  <!-- 编辑/删除按钮（仅对自己的留言显示） -->
                  <div v-if="Number(comment.user) === authStore.user?.id" class="comment-actions-menu">
                    <button
                      v-if="editingCommentId !== comment.id"
                      @click="startEditComment(comment)"
                      class="edit-comment-btn"
                    >
                      编辑
                    </button>
                    <button
                      @click="deleteComment(comment.id)"
                      class="delete-comment-btn"
                      :disabled="isDeletingComment"
                    >
                      删除
                    </button>
                  </div>
                </div>
                
                <!-- 留言内容 -->
                <div v-if="editingCommentId !== comment.id" class="comment-content">
                  {{ comment.content }}
                </div>
                
                <!-- 编辑表单 -->
                <div v-else class="edit-comment-form">
                  <textarea
                    v-model="editingCommentContent"
                    class="edit-comment-textarea"
                    rows="3"
                    maxlength="1000"
                  ></textarea>
                  <div class="edit-comment-actions">
                    <button
                      @click="saveEditComment(comment.id)"
                      :disabled="!editingCommentContent.trim() || editingCommentContent.trim().length < 5 || isUpdatingComment"
                      class="save-edit-btn"
                    >
                      保存
                    </button>
                    <button
                      @click="cancelEditComment"
                      class="cancel-edit-btn"
                    >
                      取消
                    </button>
                  </div>
                </div>
              </div>
              
              <!-- 分页控制 -->
              <div v-if="commentsPagination.total_pages > 1" class="comments-pagination">
                <button
                  @click="loadCommentsPage(commentsPagination.page - 1)"
                  :disabled="commentsPagination.page <= 1 || isLoadingComments"
                  class="pagination-btn"
                >
                  上一页
                </button>
                
                <span class="pagination-info">
                  第 {{ commentsPagination.page }} 页，共 {{ commentsPagination.total_pages }} 页
                </span>
                
                <button
                  @click="loadCommentsPage(commentsPagination.page + 1)"
                  :disabled="commentsPagination.page >= commentsPagination.total_pages || isLoadingComments"
                  class="pagination-btn"
                >
                  下一页
                </button>
              </div>
            </div>
            
            <div v-else class="no-comments">
              <p>暂无留言，快来发表第一条留言吧！</p>
            </div>
          </div>
        </div>
        
        <!-- 推荐相似电影 -->
        <div class="similar-movies-section">
          <h2>相似电影推荐</h2>
          
          <div v-if="similarMovies.length > 0" class="similar-movies-grid">
            <div
              v-for="movie in similarMovies"
              :key="movie.id"
              class="similar-movie-card"
              @click="goToMovie(movie.id)"
            >
              <h3 class="similar-movie-title">{{ movie.title }}</h3>
              <p class="similar-movie-year">{{ movie.year }}</p>
              <div class="similar-movie-genres">
                <span
                  v-for="genre in movie.genres.slice(0, 3)"
                  :key="genre.id"
                  class="genre-tag small"
                >
                  {{ genre.name }}
                </span>
              </div>
              <div class="similar-movie-rating">
                <span class="rating-value">{{ movie.average_rating.toFixed(1) }}</span>
              </div>
            </div>
          </div>
          
          <div v-else class="no-similar-movies">
            <p>暂无相似电影推荐</p>
          </div>
        </div>
      </div>
      
      <!-- 错误状态 -->
      <div v-else class="error-state">
        <p>电影详情加载失败</p>
        <button @click="loadMovieDetail" class="retry-btn">
          重试
        </button>
      </div>
    </main>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { useMoviesStore } from '@/stores/movies'
import { useRatingsStore } from '@/stores/ratings'
import type { Movie } from '@/types'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()
const moviesStore = useMoviesStore()
const ratingsStore = useRatingsStore()

// 组件状态
const newRating = ref('')
const similarMovies = ref<Movie[]>([])

// 留言相关状态
const newComment = ref('')
const comments = ref<any[]>([])
const isLoadingComments = ref(false)
const isSubmittingComment = ref(false)
const isDeletingComment = ref(false)
const isUpdatingComment = ref(false)
const commentError = ref('')
const editingCommentId = ref<number | null>(null)
const editingCommentContent = ref('')
const commentsPagination = ref({
  page: 1,
  total_pages: 1,
  count: 0
})

// 计算属性
const movieId = computed(() => Number(route.params.id))
const userRating = computed(() => {
  if (moviesStore.currentMovie) {
    return ratingsStore.getUserRatingForMovie(moviesStore.currentMovie.id)
  }
  return null
})

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 处理登出
const handleLogout = async () => {
  await authStore.logout()
  router.push({ name: 'login' })
}

// 跳转到其他电影
const goToMovie = (id: number) => {
  router.push({ name: 'movie-detail', params: { id } })
}

// 提交评分
const submitRating = async () => {
  if (!newRating.value || !moviesStore.currentMovie) return
  
  try {
    await ratingsStore.rateMovie(
      moviesStore.currentMovie.id,
      Number(newRating.value)
    )
    
    // 重新获取评分列表以更新本地状态
    await ratingsStore.fetchRatings()
    
    // 重新加载电影详情以更新平均评分
    await loadMovieDetail()
    
    newRating.value = ''
  } catch (error) {
    console.error('评分失败:', error)
  }
}

// 删除评分
const deleteRating = async () => {
  if (!userRating.value) return
  
  try {
    await ratingsStore.deleteRating(userRating.value.id)
    
    // 重新获取评分列表以更新本地状态
    await ratingsStore.fetchRatings()
    
    // 重新加载电影详情以更新平均评分
    await loadMovieDetail()
  } catch (error) {
    console.error('删除评分失败:', error)
  }
}

// 加载电影详情
const loadMovieDetail = async () => {
  try {
    await moviesStore.fetchMovie(movieId.value)
    await loadSimilarMovies()
  } catch (error) {
    console.error('加载电影详情失败:', error)
  }
}

// 加载相似电影（基于类型）
const loadSimilarMovies = async () => {
  if (!moviesStore.currentMovie || moviesStore.currentMovie.genres.length === 0) {
    return
  }
  
  try {
    // 获取同类型的电影
    const genreName = moviesStore.currentMovie.genres[0].name
    const response = await moviesStore.fetchMovies({ genre: genreName })
    
    // 过滤掉当前电影，只取前6个
    similarMovies.value = response.results
      .filter(movie => movie.id !== moviesStore.currentMovie?.id)
      .slice(0, 6)
  } catch (error) {
    console.error('加载相似电影失败:', error)
  }
}

// 格式化日期
const formatDate = (dateString: string) => {
  return new Date(dateString).toLocaleString('zh-CN')
}

// 加载电影留言
const loadComments = async (page: number = 1) => {
  if (!moviesStore.currentMovie) return
  
  try {
    isLoadingComments.value = true
    commentError.value = ''
    
    const response = await fetch(`http://127.0.0.1:8000/api/movies/${moviesStore.currentMovie.id}/comments/?page=${page}&page_size=10`, {
      headers: {
        'Authorization': `Token ${authStore.token}`,
        'Content-Type': 'application/json'
      }
    })
    
    if (!response.ok) {
      throw new Error('加载留言失败')
    }
    
    const data = await response.json()
    comments.value = data.results
    commentsPagination.value = {
      page: data.page,
      total_pages: data.total_pages,
      count: data.count
    }
  } catch (error) {
    console.error('加载留言失败:', error)
    commentError.value = '加载留言失败，请稍后重试'
  } finally {
    isLoadingComments.value = false
  }
}

// 分页加载留言
const loadCommentsPage = (page: number) => {
  loadComments(page)
}

// 提交留言
const submitComment = async () => {
  if (!newComment.value.trim() || !moviesStore.currentMovie) return
  
  try {
    isSubmittingComment.value = true
    commentError.value = ''
    
    const response = await fetch(`http://127.0.0.1:8000/api/movies/${moviesStore.currentMovie.id}/comments/`, {
      method: 'POST',
      headers: {
        'Authorization': `Token ${authStore.token}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        content: newComment.value.trim()
      })
    })
    
    if (!response.ok) {
      const errorData = await response.json()
      throw new Error(errorData.content?.[0] || '发布留言失败')
    }
    
    // 清空表单
    newComment.value = ''
    
    // 重新加载留言列表
    await loadComments(1)
  } catch (error: any) {
    console.error('发布留言失败:', error)
    commentError.value = error.message || '发布留言失败，请稍后重试'
  } finally {
    isSubmittingComment.value = false
  }
}

// 开始编辑留言
const startEditComment = (comment: any) => {
  editingCommentId.value = comment.id
  editingCommentContent.value = comment.content
}

// 取消编辑留言
const cancelEditComment = () => {
  editingCommentId.value = null
  editingCommentContent.value = ''
}

// 保存编辑的留言
const saveEditComment = async (commentId: number) => {
  if (!editingCommentContent.value.trim()) return
  
  try {
    isUpdatingComment.value = true
    commentError.value = ''
    
    const response = await fetch(`http://127.0.0.1:8000/api/comments/${commentId}/`, {
      method: 'PUT',
      headers: {
        'Authorization': `Token ${authStore.token}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        content: editingCommentContent.value.trim()
      })
    })
    
    if (!response.ok) {
      const errorData = await response.json()
      throw new Error(errorData.content?.[0] || '更新留言失败')
    }
    
    // 取消编辑状态
    cancelEditComment()
    
    // 重新加载留言列表
    await loadComments(commentsPagination.value.page)
  } catch (error: any) {
    console.error('更新留言失败:', error)
    commentError.value = error.message || '更新留言失败，请稍后重试'
  } finally {
    isUpdatingComment.value = false
  }
}

// 删除留言
const deleteComment = async (commentId: number) => {
  if (!confirm('确定要删除这条留言吗？')) return
  
  try {
    isDeletingComment.value = true
    commentError.value = ''
    
    const response = await fetch(`http://127.0.0.1:8000/api/comments/${commentId}/`, {
      method: 'DELETE',
      headers: {
        'Authorization': `Token ${authStore.token}`
      }
    })
    
    if (!response.ok) {
      throw new Error('删除留言失败')
    }
    
    // 重新加载留言列表
    await loadComments(commentsPagination.value.page)
  } catch (error) {
    console.error('删除留言失败:', error)
    commentError.value = '删除留言失败，请稍后重试'
  } finally {
    isDeletingComment.value = false
  }
}

// 监听路由变化
watch(
  () => route.params.id,
  (newId) => {
    if (newId) {
      loadMovieDetail()
    }
  }
)

// 页面初始化
onMounted(async () => {
  try {
    // 确保已加载用户评分
    if (ratingsStore.ratings.length === 0) {
      await ratingsStore.fetchRatings()
    }
    
    await loadMovieDetail()
    
    // 加载留言
    await loadComments()
  } catch (error) {
    console.error('初始化失败:', error)
  }
})

// 监听路由变化，重新加载留言
watch(() => route.params.id, async (newId) => {
  if (newId) {
    await loadComments()
  }
})
</script>

<style scoped>
.movie-detail-view {
  min-height: 100vh;
  background-color: #f8fafc;
}

/* 导航栏样式 */
.navbar {
  background: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 70px;
}

.nav-brand h2 {
  color: #667eea;
  margin: 0;
  font-size: 24px;
  font-weight: 600;
}

.nav-menu {
  display: flex;
  gap: 30px;
}

.nav-link {
  color: #64748b;
  text-decoration: none;
  font-weight: 500;
  padding: 8px 16px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

/* 导航栏样式 */
.navbar {
  background: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 70px;
}

.nav-brand h2 {
  color: #667eea;
  margin: 0;
  font-size: 24px;
  font-weight: 600;
}

.nav-menu {
  display: flex;
  gap: 30px;
}

.nav-link {
  color: #64748b;
  text-decoration: none;
  font-weight: 500;
  padding: 8px 16px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.nav-link:hover,
.nav-link.active {
  color: #667eea;
  background-color: #f1f5f9;
}

.nav-user {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-name {
  color: #475569;
  font-weight: 500;
}

.logout-btn {
  padding: 8px 16px;
  background-color: #ef4444;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: background-color 0.3s ease;
}

.logout-btn:hover {
  background-color: #dc2626;
}



/* 主要内容样式 */
.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
}

/* 返回按钮 */
.back-button {
  margin-bottom: 30px;
}

.back-btn {
  padding: 10px 20px;
  background-color: #64748b;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: background-color 0.3s ease;
}

.back-btn:hover {
  background-color: #475569;
}

/* 电影详情 */
.movie-detail {
  display: flex;
  flex-direction: column;
  gap: 40px;
}

.movie-header {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  padding: 40px;
}

.movie-title {
  color: #1e293b;
  font-size: 36px;
  font-weight: 700;
  margin: 0 0 10px 0;
  line-height: 1.2;
}

.movie-year {
  color: #64748b;
  font-size: 18px;
  margin: 0 0 20px 0;
}

.movie-genres {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 30px;
}

.genre-tag {
  background-color: #e2e8f0;
  color: #475569;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
}

.genre-tag.small {
  padding: 4px 8px;
  font-size: 12px;
}

.movie-stats {
  display: flex;
  gap: 40px;
  flex-wrap: wrap;
}

.stat-item {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.stat-label {
  color: #64748b;
  font-size: 14px;
  font-weight: 500;
}

.stat-value {
  color: #1e293b;
  font-size: 20px;
  font-weight: 600;
}

.stat-value.rating {
  color: #f59e0b;
}

/* 评分区域 */
.rating-section {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  padding: 30px;
}

.rating-section h2 {
  color: #1e293b;
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 20px 0;
}

.current-rating {
  background-color: #f0f9ff;
  border: 1px solid #bae6fd;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 20px;
}

.current-rating p {
  margin: 0 0 5px 0;
  color: #0369a1;
}

.rating-time {
  font-size: 14px;
  color: #64748b;
}

.rating-form label {
  display: block;
  color: #374151;
  font-weight: 500;
  margin-bottom: 10px;
}

.rating-input {
  display: flex;
  gap: 15px;
  align-items: center;
  flex-wrap: wrap;
}

.rating-select {
  padding: 10px 12px;
  border: 2px solid #e2e8f0;
  border-radius: 6px;
  font-size: 16px;
  background-color: white;
  cursor: pointer;
  transition: border-color 0.3s ease;
}

.rating-select:focus {
  outline: none;
  border-color: #667eea;
}

.submit-rating-btn {
  padding: 10px 20px;
  background-color: #10b981;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: background-color 0.3s ease;
}

.submit-rating-btn:hover:not(:disabled) {
  background-color: #059669;
}

.submit-rating-btn:disabled {
  background-color: #cbd5e1;
  cursor: not-allowed;
}

.delete-rating-btn {
  padding: 10px 20px;
  background-color: #ef4444;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: background-color 0.3s ease;
}

.delete-rating-btn:hover:not(:disabled) {
  background-color: #dc2626;
}

.delete-rating-btn:disabled {
  background-color: #cbd5e1;
  cursor: not-allowed;
}

/* 相似电影区域 */
.similar-movies-section {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  padding: 30px;
}

.similar-movies-section h2 {
  color: #1e293b;
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 20px 0;
}

.similar-movies-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
}

.similar-movie-card {
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.similar-movie-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.similar-movie-title {
  color: #1e293b;
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 5px 0;
  line-height: 1.3;
}

.similar-movie-year {
  color: #64748b;
  font-size: 14px;
  margin: 0 0 10px 0;
}

.similar-movie-genres {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-bottom: 10px;
}

.similar-movie-rating {
  text-align: right;
}

.similar-movie-rating .rating-value {
  color: #f59e0b;
  font-weight: 600;
  font-size: 16px;
}

/* 错误和加载状态 */
.loading,
.error-state,
.no-similar-movies {
  text-align: center;
  padding: 60px 20px;
}

.loading-spinner {
  width: 50px;
  height: 50px;
  border: 4px solid #e2e8f0;
  border-top: 4px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 20px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.error-message {
  background-color: #fee;
  color: #c33;
  padding: 12px;
  border-radius: 6px;
  margin-top: 15px;
  font-size: 14px;
  border: 1px solid #fcc;
}

.retry-btn {
  padding: 12px 24px;
  background-color: #667eea;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  transition: background-color 0.3s ease;
}

.retry-btn:hover {
  background-color: #5a67d8;
}

/* 留言区域样式 */
.comments-section {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  padding: 30px;
}

.comments-section h2 {
  color: #1e293b;
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 20px 0;
}

/* 留言表单样式 */
.comment-form {
  background-color: #f8fafc;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 30px;
}

.comment-form h3 {
  color: #374151;
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 15px 0;
}

.form-group {
  margin-bottom: 15px;
}

.comment-textarea {
  width: 100%;
  padding: 12px;
  border: 2px solid #e2e8f0;
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
  min-height: 100px;
  transition: border-color 0.3s ease;
}

.comment-textarea:focus {
  outline: none;
  border-color: #667eea;
}

.comment-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
}

.char-count {
  color: #64748b;
  font-size: 12px;
}

.submit-comment-btn {
  padding: 10px 20px;
  background-color: #10b981;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: background-color 0.3s ease;
}

.submit-comment-btn:hover:not(:disabled) {
  background-color: #059669;
}

.submit-comment-btn:disabled {
  background-color: #cbd5e1;
  cursor: not-allowed;
}

/* 留言列表样式 */
.comments-list {
  margin-top: 20px;
}

.loading-comments {
  text-align: center;
  padding: 40px 20px;
}

.comments-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.comment-item {
  background-color: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 20px;
  transition: box-shadow 0.3s ease;
}

.comment-item:hover {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  flex-wrap: wrap;
  gap: 10px;
}

.comment-author {
  color: #1e293b;
  font-weight: 600;
  font-size: 14px;
}

.comment-time {
  color: #64748b;
  font-size: 12px;
}

.comment-actions-menu {
  display: flex;
  gap: 8px;
}

.edit-comment-btn,
.delete-comment-btn {
  padding: 4px 8px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  transition: background-color 0.3s ease;
}

.edit-comment-btn {
  background-color: #3b82f6;
  color: white;
}

.edit-comment-btn:hover {
  background-color: #2563eb;
}

.delete-comment-btn {
  background-color: #ef4444;
  color: white;
}

.delete-comment-btn:hover:not(:disabled) {
  background-color: #dc2626;
}

.delete-comment-btn:disabled {
  background-color: #cbd5e1;
  cursor: not-allowed;
}

.comment-content {
  color: #374151;
  line-height: 1.6;
  white-space: pre-wrap;
  word-wrap: break-word;
}

/* 编辑留言表单样式 */
.edit-comment-form {
  margin-top: 10px;
}

.edit-comment-textarea {
  width: 100%;
  padding: 10px;
  border: 2px solid #e2e8f0;
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
  transition: border-color 0.3s ease;
}

.edit-comment-textarea:focus {
  outline: none;
  border-color: #667eea;
}

.edit-comment-actions {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.save-edit-btn {
  padding: 8px 16px;
  background-color: #10b981;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  transition: background-color 0.3s ease;
}

.save-edit-btn:hover:not(:disabled) {
  background-color: #059669;
}

.save-edit-btn:disabled {
  background-color: #cbd5e1;
  cursor: not-allowed;
}

.cancel-edit-btn {
  padding: 8px 16px;
  background-color: #64748b;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  transition: background-color 0.3s ease;
}

.cancel-edit-btn:hover {
  background-color: #475569;
}

/* 留言分页样式 */
.comments-pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 20px;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #e2e8f0;
}

.no-comments {
  text-align: center;
  padding: 40px 20px;
  color: #64748b;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-container {
    flex-direction: column;
    height: auto;
    padding: 15px 20px;
  }
  
  .nav-menu {
    margin: 15px 0;
  }
  
  .movie-title {
    font-size: 28px;
  }
  
  .movie-stats {
    gap: 20px;
  }
  
  .rating-input {
    flex-direction: column;
    align-items: stretch;
  }
  
  .similar-movies-grid {
    grid-template-columns: 1fr;
  }
  
  .comment-header {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .comment-actions {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  
  .comments-pagination {
    flex-direction: column;
    gap: 10px;
  }
}
</style>